﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OLTS.DATA_SERVICE;
using OLTS.MODEL.Custom_Model;
using OLTS.MODEL;
using OLTS.WEB.BaseClass;
 

namespace ONTS.WEB.Views
{
    public partial class TimeSheetKeyIn : BaseWebpage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                initialData();
                Session["detail"] = null;
            }
        }

        protected void BtnSaveData_Click(object sender, EventArgs e)
        {
            var timesheet = (List<OLTS.MODEL.OTS_TIME_SHEET>)Session["detail"];

            foreach (var o in timesheet)
            {
                if (o.Action == ActionEnum.Create)
                {
                    var cmd = new TimeSheet_Service(o);
                    cmd.Add();
                }
                else if (o.Action == ActionEnum.Update)
                {
                    var cmd = new TimeSheet_Service(o);
                    cmd.Edit();
                }
                else if (o.Action == ActionEnum.Delete)
                {
                    var cmd = new TimeSheet_Service(o);
                    cmd.Delete();
                }

            }

            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Save Data Successfully')", true);

            Session["detail"] = null;
        }

        protected void BtnReset_Click(object sender, EventArgs e)
        {
            Session["detail"] = null;

        }

        private void initialData()
        {
            isHoliday();
            getLeaveDDL();
            BindDate();
            BindTime();
            ddlEndTime.SelectedValue = "17:30";
            ddlStartTime.SelectedValue = "8:30";
        }

        private void BindTime()
        {
            var starttime = DateTime.Today.AddHours(8).AddMinutes(30);
            var endtime = starttime.AddHours(14);
            while (starttime <= endtime)
            {
                ddlStartTime.Items.Add(starttime.ToString("HH:mm"));
                ddlEndTime.Items.Add(starttime.AddHours(9).ToString("HH:mm"));
                starttime = starttime.AddMinutes(30);
            }
        }

        private void getLeaveDDL()
        {
            ddlLeave.Items.Clear();
            ddlLeave.Items.Add(new ListItem("Please select leave type", "0"));
            var cmd = new LEAVE_Service();
            var result = cmd.GetAll();
            foreach (var o in result)
            {
                ddlLeave.Items.Add(new ListItem(o.TYPE, o.NAME));
            }

        }

        private void BindDate()
        { 
            var date = DateTime.Now;
            for (int i = 0; i < 3; i++)
            {
                ddlDate.Items.Add(date.AddDays(-i).ToString("dd-MM-yyyy"));
            }
        }

        private bool isHoliday()
        {
            txtholiday.Enabled = false;
            var currentuser = BaseService.LOGIN.EMPLOYEE_ID;
            var site = BaseService.PROJECT.SITE_ID;
            var yr = DateTime.Now.Year.ToString();
            var cmd = new Holiday_Service(yr, Convert.ToInt16(site));
            var result = cmd.GetListHoliday();
            var isholiday = result.Where(x => x.DATE == DateTime.Now.Date).FirstOrDefault();
            if (isholiday != null)
            {
                txtholiday.Text = "Holiday : " + isholiday.NAME;

                return true;
            }
            else
            {
                return false;
            }

        }

        protected void ddlDate_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}